<template>
  <div>
    <!-- Sidebar -->
    <div class="sidebar">
      <div style="padding: 24px 20px; border-bottom: 1px solid #e5e7eb;">
        <div style="display: flex; align-items: center;">
          <div style="width: 40px; height: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: 18px;">快</div>
          <div style="margin-left: 12px;">
            <div style="font-weight: 600; color: #111827;">快递代拿管理系统</div>
          </div>
        </div>
      </div>

      <div style="padding: 16px 0;">
        <div class="nav-section">主菜单</div>
        <button :class="['nav-item', $route.path === '/admin/dashboard' ? 'active' : '']" @click="$router.push('/admin/dashboard')"><i class="fas fa-chart-line"></i><span>数据总览</span></button>
        <button :class="['nav-item', $route.path === '/admin/orders' ? 'active' : '']" @click="$router.push('/admin/orders')"><i class="fas fa-box"></i><span>订单管理</span></button>
        <button :class="['nav-item', $route.path === '/admin/users' ? 'active' : '']" @click="$router.push('/admin/users')"><i class="fas fa-users"></i><span>下单者管理</span></button>
        <button :class="['nav-item', $route.path === '/admin/couriers' ? 'active' : '']" @click="$router.push('/admin/couriers')"><i class="fas fa-motorcycle"></i><span>代拿者管理</span></button>
        <button :class="['nav-item', $route.path === '/admin/finance' ? 'active' : '']" @click="$router.push('/admin/finance')"><i class="fas fa-wallet"></i><span>财务管理</span></button>

        <div class="nav-section">运营管理</div>
        <button :class="['nav-item', $route.path === '/admin/announcements' ? 'active' : '']" @click="$router.push('/admin/announcements')"><i class="fas fa-bullhorn"></i><span>公告管理</span></button>
        <button :class="['nav-item', $route.path === '/admin/pricing' ? 'active' : '']" @click="$router.push('/admin/pricing')"><i class="fas fa-percentage"></i><span>盈利规则</span></button>
        

        <div class="nav-section">系统设置</div>
        <button :class="['nav-item', $route.path === '/admin/permissions' ? 'active' : '']" @click="$router.push('/admin/permissions')"><i class="fas fa-shield-alt"></i><span>权限管理</span></button>
        <button :class="['nav-item', $route.path === '/admin/logs' ? 'active' : '']" @click="$router.push('/admin/logs')"><i class="fas fa-file-alt"></i><span>系统日志</span></button>
        <button :class="['nav-item', $route.path === '/admin/settings' ? 'active' : '']" @click="$router.push('/admin/settings')"><i class="fas fa-cog"></i><span>系统设置</span></button>
      </div>

      <div style="position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; border-top: 1px solid #e5e7eb;">
        <div style="display: flex; align-items: center;">
          <div style="width: 40px; height: 40px; border-radius: 50%; background:#e5e7eb; display:flex; align-items:center; justify-content:center; color:#6b7280; font-weight:600;">管</div>
          <div style="margin-left: 12px; flex: 1;">
            <div style="font-weight: 500; color: #111827; font-size: 14px;">管理员</div>
            <div style="font-size: 12px; color: #9ca3af;">超级管理员</div>
          </div>
        </div>
        <button class="nav-item" @click="confirmLogout" style="margin-top:12px;"><i class="fas fa-sign-out-alt"></i><span>退出登录</span></button>
      </div>
    </div>

    <!-- Main Content -->
    <div class="main-content">
      <!-- Header -->
      <div style="background: white; border-bottom: 1px solid #e5e7eb; padding: 16px 32px; display: flex; align-items: center; justify-content: space-between;">
        <div></div>
        <div style="position: relative;">
          <i class="fas fa-search" style="position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #9ca3af;"></i>
          <input type="text" placeholder="搜索..." style="padding: 8px 12px 8px 36px; border: 1px solid #e5e7eb; border-radius: 8px; width: 240px; font-size: 14px;">
        </div>
      </div>

      <!-- Content -->
      <div style="padding: 32px;">
        <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 32px;">
          <div>
            <h1 style="font-size: 28px; font-weight: 700; color: #111827; margin-bottom: 8px;">权限管理</h1>
            <p style="color: #6b7280;">管理系统角色和权限配置</p>
          </div>
          <button @click="openCreateRoleDialog" style="padding: 12px 24px; background: #3b82f6; color: white; border: none; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500;"><i class="fas fa-plus"></i><span>添加角色</span></button>
        </div>

        <!-- Role Cards -->
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 24px; margin-bottom: 32px;">
          <div v-for="role in roles" :key="role.id" class="role-card" :style="role.id === 1 ? 'border-color: #3b82f6; background: linear-gradient(135deg, #eff6ff 0%, #ffffff 100%);' : ''">
            <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px;">
              <div style="display: flex; align-items: center; gap: 12px;">
                <div :style="`width: 48px; height: 48px; background: ${getRoleColor(role.id)}; border-radius: 10px; display: flex; align-items: center; justify-content: center;`">
                  <i :class="`fas ${getRoleIcon(role.id)}`" style="font-size: 20px; color: white;"></i>
                </div>
                <div>
                  <h3 style="font-size: 18px; font-weight: 600; color: #111827;">{{ role.name }}</h3>
                  <span class="status-badge status-active">启用</span>
                </div>
              </div>
            </div>
            <p style="color: #6b7280; font-size: 14px; margin-bottom: 16px;">{{ role.description || '暂无描述' }}</p>
            <div style="display: flex; align-items: center; justify-content: space-between; padding-top: 16px; border-top: 1px solid #e5e7eb;">
              <span style="color: #6b7280; font-size: 13px;">用户数：{{ role.user_count || 0 }}人</span>
              <button @click="editRole(role)" style="padding: 6px 16px; background: #3b82f6; color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 13px;">编辑</button>
            </div>
          </div>
          <div v-if="roles.length === 0" style="text-align: center; padding: 40px; color: #9ca3af; grid-column: 1/-1;">暂无角色</div>
        </div>

        <!-- Permission Matrix -->
        <div class="content-card">
          <h3 style="font-size: 18px; font-weight: 600; color: #111827; margin-bottom: 20px;">权限矩阵</h3>
          <div style="overflow-x: auto;">
            <table>
              <thead>
                <tr>
                  <th>功能模块</th>
                  <th v-for="role in roles" :key="role.id" style="text-align: center;">{{ role.name }}</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="permission in permissions" :key="permission.id">
                  <td style="font-weight: 500;">{{ permission.name }}</td>
                  <td v-for="role in roles" :key="role.id" style="text-align: center;">
                    <i 
                      v-if="hasPermission(role.id, permission.id)"
                      class="fas fa-check-circle" 
                      style="color: #10b981; font-size: 18px;"
                    ></i>
                    <i 
                      v-else
                      class="fas fa-times-circle" 
                      style="color: #ef4444; font-size: 18px;"
                    ></i>
                  </td>
                </tr>
                <tr v-if="permissions.length === 0">
                  <td :colspan="roles.length + 1" style="text-align: center; padding: 40px; color: #9ca3af;">暂无权限数据</td>
                </tr>
              </tbody>
            </table>
          </div>
          <div style="display: flex; gap: 24px; margin-top: 20px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
            <div style="display: flex; align-items: center; gap: 8px;"><i class="fas fa-check-circle" style="color: #10b981; font-size: 16px;"></i><span style="color: #6b7280; font-size: 13px;">完全权限</span></div>
            <div style="display: flex; align-items: center; gap: 8px;"><i class="fas fa-eye" style="color: #6b7280; font-size: 16px;"></i><span style="color: #6b7280; font-size: 13px;">只读权限</span></div>
            <div style="display: flex; align-items: center; gap: 8px;"><i class="fas fa-times-circle" style="color: #ef4444; font-size: 16px;"></i><span style="color: #6b7280; font-size: 13px;">无权限</span></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  name: 'AdminPermissions',
  data() {
    return {
      roles: [],
      permissions: [],
      permissionMatrix: {},
      showRoleDialog: false,
      editingRole: null,
      roleForm: {
        name: '',
        description: ''
      }
    }
  },
  methods: {
    confirmLogout() {
      if (confirm('是否确认退出？')) {
        localStorage.removeItem('adminToken')
        localStorage.removeItem('adminInfo')
        localStorage.removeItem('userToken')
        this.$router.push('/login')
      }
    },
    getRoleColor(roleId) {
      const colors = {
        1: '#3b82f6',
        2: '#10b981',
        3: '#f59e0b',
        4: '#a855f7'
      }
      return colors[roleId] || '#6b7280'
    },
    getRoleIcon(roleId) {
      const icons = {
        1: 'fa-crown',
        2: 'fa-user-tie',
        3: 'fa-wallet',
        4: 'fa-headset'
      }
      return icons[roleId] || 'fa-user'
    },
    async loadPermissions() {
      try {
        const { data } = await axios.get('/api/admin/permissions')
        if (data && data.success) {
          this.roles = data.data.roles || []
          this.permissions = data.data.permissions || []
          this.permissionMatrix = data.data.permissionMatrix || {}
        }
      } catch (e) {
        console.error('加载权限信息失败', e)
        ElMessage.error('加载权限信息失败')
      }
    },
    openCreateRoleDialog() {
      this.editingRole = null
      this.roleForm = { name: '', description: '' }
      this.showRoleDialog = true
    },
    editRole(role) {
      this.editingRole = role
      this.roleForm = { name: role.name, description: role.description || '' }
      this.showRoleDialog = true
    },
    hasPermission(roleId, permissionId) {
      return this.permissionMatrix[roleId] && this.permissionMatrix[roleId].includes(permissionId)
    }
  },
  mounted() {
    this.loadPermissions()
  }
}
</script>

<style scoped>
.sidebar { position: fixed; left: 0; top: 0; width: 240px; height: 100vh; background: #fff; border-right: 1px solid #e5e7eb; overflow-y: auto; }
.main-content { margin-left: 240px; min-height: 100vh; }
.nav-item { display: flex; align-items: center; padding: 12px 20px; color: #6b7280; text-decoration: none; transition: all 0.2s; cursor: pointer; width: 100%; background: transparent; border: 0; text-align: left; }
.nav-item:hover { background: #f3f4f6; color: #3b82f6; }
.nav-item.active { background: #eff6ff; color: #3b82f6; font-weight: 500; }
.nav-item i { width: 20px; margin-right: 12px; }
.nav-section { padding: 16px 20px 8px; font-size: 12px; color: #9ca3af; font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px; }
.content-card { background: white; border-radius: 12px; padding: 24px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 24px; }
.role-card { border: 2px solid #e5e7eb; border-radius: 12px; padding: 20px; transition: all 0.2s; }
.role-card:hover { border-color: #3b82f6; box-shadow: 0 4px 12px rgba(59, 130, 246, 0.1); }
.status-badge { display: inline-block; padding: 4px 12px; border-radius: 12px; font-size: 12px; font-weight: 500; }
.status-active { background: #d1fae5; color: #065f46; }
.status-inactive { background: #f3f4f6; color: #6b7280; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; padding: 12px; color: #6b7280; font-weight: 500; font-size: 13px; border-bottom: 1px solid #e5e7eb; }
 td { padding: 16px 12px; border-bottom: 1px solid #f3f4f6; color: #374151; }
</style>
